/// <reference path="_namespace.js" />

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
For use server side.
Class: ajax.data.Serialization
/*****************************************************************************/


// "Static object", does NOT inherit from ajax.BaseObject.

ajax.lang.addNamespace("ajax.data");

//-----------------------------------------------------------------------------
ajax.data.Serialization = function(){}

//-----------------------------------------------------------------------------
ajax.data.Serialization.prototype.toString = function()
{
    return "ajax.data.Serialization";    
}

//-----------------------------------------------------------------------------
ajax.data.Serialization.xmlMessage = function(method, rs)
{
    return "<object type=\"object\">"
         + "    <method type=\"string\">" + method + "</method>"
         +  ajax.data.Serialization.rsToXmlString(rs, "data") 
         + "</object>";
}

//-----------------------------------------------------------------------------
ajax.data.Serialization.jsonMessage = function(method, rs)
{
    var xmlString   = ajax.data.Serialization.rsToXmlString(rs);
    var data        = ajax.lang.xmlToJsonObject(xmlString);
    var dataJson    = ajax.lang.toJsonString(data);    
    return "{method: '" + method + "', data: " + dataJson + "}";
}

//-----------------------------------------------------------------------------
// Converts an ADODB.Recordset Row to an XML String
ajax.data.Serialization.rsRowToXmlString = function(rs)
{    
    var ret = "";
    if (rs.EOF || rs.BOF)
        return "";
        
    for (var i = 0; i < rs.Fields.Count; i++)
    {
        var fld     = rs.Fields.Item(i);        
        var sType   = "string"; //??
        var sName   = fld.Name;        
        var sValue  = "";        
        if (fld)
        {            
            sValue = fld.Value;            
            // Convert booleans to their true representation
            if (sValue == "False")
                sValue = false;
            if (sValue == "True")
                sValue = true;
            
            // Now get type and then encode it (if it's a string)
            sType = ajax.lang.getType(sValue);
            sValue    = ajax.lang.htmlEncode(sValue, false, 4);
                      
            ret += "<" + sName + " type=\"" + sType + "\">" + sValue + "</" + sName + ">\n";              
        }       
      
    }
    
    return ret;
}

//-----------------------------------------------------------------------------
// Converts an ADODB.Recordset to an XML String
ajax.data.Serialization.rsToXmlString = function(rs, sName)
{
    sName = sName || "object";
    var ret = "<" + sName + " type=\"array\">\n";
    
    while (rs.EOF == false)
    {
        var sItem = "<object type=\"object\">" + ajax.data.Serialization.rsRowToXmlString(rs) + "</object>";
        ret += sItem;
        rs.MoveNext();
    } 
    ret += "\n</" + sName + ">";
    return ret;
}

//-----------------------------------------------------------------------------
// Converts an ADODB.Recordset to a Json String
ajax.data.Serialization.rsToJsonString = function(rs, sName)
{
    var xml = ajax.data.Serialization.rsToXmlString(rs, sName);
    var obj = ajax.lang.xmlToJsonObject(xml);
    return ajax.lang.toJsonString(obj);
}

//-----------------------------------------------------------------------------
// Pass in rs (at first row) followed by each column you want
ajax.data.Serialization.rsToTypeAheadJson = function(rs, args)
{
    var iCount = 0;
    var ret = "new Array(";
    while (rs.EOF == false)
    {
        if (iCount > 0)
            ret += ",";
        ret += "[";
        
        for (var i = 1; i < arguments.length; i++)
        {
            
            ret += "\"" + ajax.lang.trim(rs(arguments[i])) + "\"";
            if (i < arguments.length -1)
                ret += ",";            
        }
        ret += "]";
        rs.MoveNext();
        iCount++;
    }
    ret += ");";
    return ret;
}

//-----------------------------------------------------------------------------
ajax.data.Serialization.rsToTreeviewJson = function(rs, keyFieldName, wordsFieldName, nodeType, image, imageSel, tag)
{
    nodeType = nodeType || "Folder";
    image    = image || "";
    imageSel = imageSel || "";
    tag      = tag || "";
    // Get the recordset into a js native object
    var xmlString = ajax.data.Serialization.rsToXmlString(rs);
    var data      = xmlString.xmlToJsonObject(); 
    var results   = []; // Array to store the results
    for (var i = 0; i < data.length; i++)
    {
        // Loop through data and create an object
        var obj = 
        {
            type: nodeType,
            img: image,
            imgSel: imageSel,
            words: data[i][wordsFieldName],
            key: data[i][keyFieldName],
            tag: tag
        }       
        results.push(obj);        
    }
    return ajax.lang.toJsonString(results);
}

//-----------------------------------------------------------------------------
ajax.data.Serialization.cast = function(obj)
{///<returns type="ajax.data.Serialization"/>
    return obj;            
}